1. import libraries and and dataset
library(tidyverse)
library(ggplot2)
library(dplyr)
library(readr)
library(plotly)
library(ggfortify)
library(ggseas)
data <- read_csv("covid.com.csv") %>% 
  select(c(-1))
  1. preprocess data
# choose which state
state = "WASHINGTON"
state_data <- data %>% 
  filter(STATE==state) %>% 
  select(c(3, 9, 14, 15))

# group event categories into easing/lifting and imposing
group_event_catg <- function(x){
  new_col = c()
  check = substr(x, nchar(x), nchar(x))
  if(check %in% c("M", "R")){
    new_col <- append(new_col, "Mandatory/Recommended")
  } else if (check %in% c("E", "L")){
    new_col <- append(new_col, "Ease/Lifting")
  } else {
    new_col <- append(new_col, x)
  }
}
state_data$big_event_catg <- sapply(state_data$EVENT_CATG_S,
                                    FUN=group_event_catg)

# text color bins
bins <- c(-Inf,0,0.25,0.75,1.20,1.55,1.85,2.10,2.35,2.70,3.05,3.40,3.75,
          4.00, 5.00)
data$color_scale <- cut(data$SCORE, breaks=bins)
color_v <- c("#ffffff","#fdfec0","#ffd799","#ffb078","#fc8861","#f2615d",
             "#d8466c","#87377a","#942c81","#721f82","#51127c", "#2d1160",
             "#ffd3d1", "#100c2e")

color_event_v <- c("#ff0000","#e08c75","#f2cec4","#f2cf05","#ede02b",
                      "#ada63e","#b3b827","#144d6b", "#07a3f5",
                      "#81b7d4", "#a3bcc9","#cfeeff", "#9905f5","#c99fe3",
                      "#480c6e", "#715b80", "#d324f2", "#ffffff")
names(color_event_v) = levels(as.factor(data$EVENT_CATG_S))
names(color_v) = levels(data$color_scale)
  1. plot lag graphs
# 1 day lag
ggplot(data=state_data)+
  geom_point(aes(x=NEWCD_NORM_500, y=lead(NEWCD_NORM_500),
                 col=as.factor(big_event_catg)))+
  labs(title=paste(str_to_title(state),"lag 1"), 
       x="Today's Norm New Confirmed Cases",
       y="Tomorrow's Norm NC Cases",
       col = "Event_Category")+
  theme_bw()

# 3 day lag
ggplot(data=state_data)+
  geom_point(aes(x=NEWCD_NORM_500, y=lead(NEWCD_NORM_500, n=3),
                 col=as.factor(big_event_catg)))+
  labs(title=paste(str_to_title(state),"lag 3"), 
       x="Today's Norm New Confirmed Cases",
       y="Norm NC Cases 3 days later",
       col = "Event_Category")+
  theme_bw()

# 5 day lag
ggplot(data=state_data)+
  geom_point(aes(x=NEWCD_NORM_500, y=lead(NEWCD_NORM_500, n=5),
                 col=as.factor(big_event_catg)))+
  labs(title=paste(str_to_title(state),"lag 5"), 
       x="Today's Norm New Confirmed Cases",
       y="Norm NC Cases 5 days later",
       col = "Event_Category")+
  theme_bw()

# 7 day lag
ggplot(data=state_data)+
  geom_point(aes(x=NEWCD_NORM_500, y=lead(NEWCD_NORM_500, n=7),
                 col=as.factor(big_event_catg)))+
  labs(title=paste(str_to_title(state),"lag 7"), 
       x="Today's Norm New Confirmed Cases",
       y="Norm NC Cases 7 days later",
       col = "Event_Category")+
  theme_bw()

# 10 day lag
ggplot(data=state_data)+
  geom_point(aes(x=NEWCD_NORM_500, y=lead(NEWCD_NORM_500, n=10),
                 col=as.factor(big_event_catg)))+
  labs(title=paste(str_to_title(state),"lag 10"), 
       x="Today's Norm New Confirmed Cases",
       y="Norm NC Cases 10 days later",
       col = "Event_Category")+
  theme_bw()

  1. put into function
plot_lag <- function(state, lead_n){
  state_data <- data[data$STATE==state,]
  state_data$big_event_catg <- sapply(state_data$EVENT_CATG_S,
                                    FUN=group_event_catg)
  
  plot <- ggplot(data=state_data)+
    geom_point(aes(x=NEWCD_NORM_500, y=lead(NEWCD_NORM_500, n=lead_n),
                   col=as.factor(big_event_catg)))+
    labs(title=paste(str_to_title(state),"lag", lead_n), 
         x="Today's Norm New Confirmed Cases",
         y=paste("Norm NC Cases", lead_n, "days later"),
         col = "Event_Category")+
    theme_bw()
  
  print(plot)
}
  1. create an interactive version function for lag
plot_lag_interact <- function(state, lead_n){
  state_data <- data[data$STATE==state,]
  state_data$big_event_catg <- sapply(state_data$EVENT_CATG_S,
                                    FUN=group_event_catg)
  
  plot2 <- ggplot(data=state_data)+
    geom_point(aes(x=NEWCD_NORM_500, y=lead(NEWCD_NORM_500, n=lead_n),
                   col=as.factor(big_event_catg),
                   Date=DATE, Event=EVENT_CATG_S))+
    labs(title=paste(str_to_title(state),"lag", lead_n), 
         x="Today's Norm New Confirmed Cases",
         y=paste("Norm NC Cases", lead_n, "days later"),
         col = "Event_Category")+
    theme_bw()
  
  ggplotly(plot2, tooltip=c("x", "y", "Date", "Event"))
}
  1. Create an interactive version for the time trend
plot_time_interact <- function(state, window_num){

  state_data <- data[data$STATE==state,]
  state_event <- state_data[!is.na(state_data$EVENT_CATG_S),]
  state_event_date <- state_event$DATE
  plot3 <- ggplot(state_data, aes(DATE, NEWCD_NORM_500)) + 
    geom_point(size=2, alpha=0.7) + 
    stat_rollapplyr(width = window_num, align = "right", size = 2,
                    col="purple", alpha=0.8)+
    geom_vline(data=state_event,
               aes(xintercept=as.numeric(DATE), color=EVENT_CATG_S), 
               linetype="dotted")+
    geom_text(data=state_event,
               aes(x=DATE, y=max(NEWCD_NORM_500), color=EVENT_CATG_S,
                   label=EVENT_CATG_S))+
    labs(x = "Time index", y = "New Daily Cases Normalized")+
    ggtitle(paste("Width = ", window_num))
  ggplotly(plot3)
}

**7. run plot functions in specified state and lag days

state = "WASHINGTON"
for (day in c(1, 3, 5, 7, 10, 14, 21, 30, 60, 90)){
  plot_lag(state, day)
}

**8. run interactive lag plot functions with specified state and lag days

state = "TEXAS"
lag = 60
plot_lag_interact(state, lag)

**9. run interactive time series plot with specified state and window

state = "WASHINGTON"
window_num = 7
plot_time_interact(state, window_num)